package com.ruoyi.zyx.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.ZyxDevice;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sms.SmsUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zyx.domain.ZyxOrderBill;
import com.ruoyi.zyx.domain.ZyxOrdershare;
import com.ruoyi.zyx.domain.ZyxSendorder;
import com.ruoyi.zyx.service.IZyxDeviceService;
import com.ruoyi.zyx.service.IZyxOrderBillService;
import com.ruoyi.zyx.service.IZyxOrdershareService;
import com.ruoyi.zyx.service.IZyxSendorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/zyxBillApi")
public class ZyxBillApi extends BaseController {
    @Autowired
    private IZyxOrderBillService zyxOrderBillService;
    @Autowired
    private IZyxSendorderService zyxSendorderService;
    @Autowired
    private IZyxDeviceService zyxDeviceService;
    @Autowired
    private IZyxOrdershareService zyxOrdershareService;

    @Autowired
    private ISysUserService sysUserService;
    /**
     * 回单信息保存
     * @param zyxOrderBill
     * @return
     */
    @PostMapping("saveBillApi")
    public AjaxResult saveBillApi(@RequestBody ZyxOrderBill zyxOrderBill)
    {
        ZyxSendorder zyxSendorderquery=zyxSendorderService.selectZyxSendorderById(zyxOrderBill.getSendorderid());
        if(zyxSendorderquery.getSendtype().equals("1")){//维修
            ZyxDevice zyxDevice=new ZyxDevice();
            zyxDevice.setDevicestatus(zyxOrderBill.getDevicestatus());
            zyxDevice.setId(zyxSendorderquery.getDeviceid());
            zyxDeviceService.updateZyxDevice(zyxDevice);
        }
        Double pr=0.0;
        try {
            pr= DateUtils.getDateHours(zyxSendorderquery.getServicedate(),new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        zyxSendorderquery.setProcessdate(pr);
        zyxSendorderquery.setServicestatus("4");
        zyxSendorderquery.setOverdate(new Date());
        zyxSendorderquery.setDevicestatus(zyxOrderBill.getDevicestatus());
        //更新回执状态
        zyxSendorderquery.setIsbill("Y");
        zyxSendorderService.onlyupd(zyxSendorderquery);

        //发送完成短信
        ZyxOrdershare zyxOrdershare=new ZyxOrdershare();
        zyxOrdershare.setSendorderid(zyxSendorderquery.getId());
        List<ZyxOrdershare> zyxOrdershares=zyxOrdershareService.selectZyxOrdershareList(zyxOrdershare);
        for (ZyxOrdershare ordershare : zyxOrdershares) {
            try {
                //发送短信通知共享人 完成短信
                SysUser sysUser=sysUserService.selectUserById(ordershare.getUserid());
                SmsUtils.sendSms(sysUser.getUserName(),sysUser.getNickName(),"SMS_218896624");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return toAjax(zyxOrderBillService.insertZyxOrderBill(zyxOrderBill));
    }
    @GetMapping(value = "/getInfoBySendorderid/{sendorderid}")
    public AjaxResult getInfoBySendorderid(@PathVariable("sendorderid") Long sendorderid)
    {
        return AjaxResult.success(zyxOrderBillService.getInfoBySendorderid(sendorderid));
    }
}
